/**
 * 茶艺师
 * @constructor
 */
function Artist() {
}

Artist.init = function () {
    $('#tableList').bootstrapTable({
        url: '/AppArtist/tableSearch',
        method: 'post',
        striped : true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle : true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
        //search : "true",
        uniqueId: "id",
        pageList : [ 5, 10, 20,50,100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search:false,
        showColumns: true,
        showRefresh: true,
        //得到查询的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
                //********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
                //********
                //获取查询的对象，往下加入
                name: $('#search_name').val(), // 请求时向服务端传递的参数
                phone: $('#search_phone').val(),
            };
            return query_params;
        },
        columns: [
            {
                //field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize=$('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber=$('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            {
                field: 'imageUrl', title: '个人照片',align: 'center',width: 200,
                formatter:function(value){
                    return "<img src=\""+value+"\" class=\"img-responsive\">";
                }
            },
            { field: 'name', title: '姓名',align: 'center'},
            { field: 'phone', title: '电话',align: 'center'},
            { field: 'star', title: '星级',align: 'center'},
            { field: 'amount', title: '单价/小时',align: 'center'},
            {
                title: '激活状态',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    if (row.state == 0) {
                        optionHtml = "<span class=\"label label-danger\" style=\"cursor:pointer\"   title=\"未激活\" onclick='actionRecord(" + JSON.stringify(row) + ",\"change\")' >未激活</span>";
                    } else if (row.state == 1) {
                        optionHtml = "<span class=\"label label-info\" style=\"cursor:pointer\"   title=\"已激活\"   onclick='actionRecord(" + JSON.stringify(row) + ",\"change\")'>已激活</span>";
                    }
                    return optionHtml;
                }
            },
            {
                title: '营业状态',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    if (row.isWork == 0) {
                        optionHtml = "<span class=\"label label-danger\" style=\"cursor:pointer\"   title=\"忙碌\" onclick='actionRecord(" + JSON.stringify(row) + ",\"chan\")' >忙碌</span>";
                    } else if (row.isWork == 1) {
                        optionHtml = "<span class=\"label label-info\" style=\"cursor:pointer\"   title=\"空闲\"   onclick='actionRecord(" + JSON.stringify(row) + ",\"chan\")'>空闲</span>";
                    }
                    return optionHtml;
                }
            },
            {
                title: '认证状态',
                align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    if (row.isCertified == 0) {
                        optionHtml = "<span class=\"label label-danger\" style=\"cursor:pointer\"   title=\"未认证\" onclick='actionRecord(" + JSON.stringify(row) + ",\"certified\")' >未认证</span>";
                    } else if (row.isCertified == 1) {
                        optionHtml = "<span class=\"label label-info\" style=\"cursor:pointer\"   title=\"已认证\" >已认证</span>";
                    }
                    return optionHtml;
                }
            },
            { field: 'synopsis', title: '简介',align: 'center'},
            {
                title: '操作', align: 'center',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    optionHtml =" <a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"edit\")' title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                    /*optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='actionRecord(" + JSON.stringify(row) + ",\"delete\")'  title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>"*/
                    return optionHtml;
                }
            }
        ]
    });
}
//查询方法****************************
function doSearch() {
    $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
}
//重置****************************
function doRest() {
    $("#search_name").val("");
    $("#search_phone").val("");
    $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
}
//增加方法****************************
var pxt="/AppArtist"; //控制器名称
var Id = "";
var url="";//图片路径
var Action="";
function addRecord() {
    Id=0;
    Action="add";
    $("#myform")[0].reset();
    $("#myimage").html("");
    $("#save").removeAttr("disabled");
}
//增加数据保存****************************
function saveRecord() {
    //创建传递的参数
    var postdata = {
        action:Action,
        id:Id,
        imageUrl:url,
        name:$("#text_name").val(),
        phone:$("#text_phone").val(),
        amount:$("#text_amount").val(),
        star:$("#text_star").val(),
        synopsis:$("#text_synopsis").val(),
        lng:$("#text_lng").val(),
        lat:$("#text_lat").val(),
        scope:$("#text_scope").val(),
    };
    //发送异步请求到后台保存用户数据
    $.ajax({
        url:pxt+"/save",
        type:"post",
        data:JSON.stringify(postdata),
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.code === "200") {
                $('#myModalAdd').modal('hide')
                doSearch();
                swal("操作成功!", "请继续操作", "success");
            } else {
                swal("操作失败!", "请重试", "error");
            }
        }
    });
}
//操作相关方法****************************
function actionRecord(data,action) {
    if (action === "delete"){
        //删除
        swal({
            title: "确认是否删除",
            text: "是否确认要删除该条记录？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            //点击删除之后此处进行传递后台
            $.ajax({
                type: "delete",
                url: pxt+"/delgoods/"+data.id,
                dataType : "json",
                contentType: "application/json",
                success: function (data, status) {
                    if (data.code == "200") {
                        doSearch();
                        swal("删除成功!", "请继续操作", "success");
                    }
                    else{
                        swal("删除失败!", "请重试", "error");
                    }
                }
            });

        });
    }else if(action === "edit"){ //修改
        //修改
        Id=data.id;
        Action="edit";
        $("#myform")[0].reset();
        $("#save").removeAttr("disabled");
        //根据id查询到对象
        getImage(data.url);
        WriteData(data);
        $('#myModalAdd').modal('show')
    }else if(action=="change") {
        //状态变更
        var alertstr = "未知操作";
        var state = 0;
        if (data.state == 0){
            alertstr = "是否将该茶艺师变更为：已激活？";
            state = 1;
        } else if (data.state == 1) {
            alertstr = "是否将该茶艺师变更为：未激活？";
            state = 0;
        }
        swal({
            title: "状态变更",
            text: alertstr,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            //点击删除之后此处进行传递后台
            //创建传递的参数
            var postdata = {
                action:"change",
                id: data.id,
                state:state,
            };
            $.ajax({
                url:pxt+"/save",
                type:"post",
                data:JSON.stringify(postdata),
                contentType:"application/json",
                dataType:"json",
                success:function(data){
                    if (data.code === "200") {
                        $('#myModalAdd').modal('hide')
                        doSearch();
                        swal("状态已变更!", "请继续操作", "success");
                    } else {
                        swal("操作失败!", "请重试", "error");
                    }
                }
            })
        });
    }else if(action=="chan") {
        //状态变更
        var alertstr = "未知操作";
        var isWork = 0;
        if (data.isWork == 0){
            alertstr = "是否将该茶艺师变更为：忙碌？";
            isWork = 1;
        } else if (data.isWork == 1) {
            alertstr = "是否将该茶艺师变更为：空闲？";
            isWork = 0;
        }
        swal({
            title: "状态变更",
            text: alertstr,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            //点击删除之后此处进行传递后台
            //创建传递的参数
            var postdata = {
                action:"chan",
                id: data.id,
                isWork:isWork,
            };
            $.ajax({
                url:pxt+"/save",
                type:"post",
                data:JSON.stringify(postdata),
                contentType:"application/json",
                dataType:"json",
                success:function(data){
                    if (data.code === "200") {
                        $('#myModalAdd').modal('hide')
                        doSearch();
                        swal("状态已变更!", "请继续操作", "success");
                    } else {
                        swal("操作失败!", "请重试", "error");
                    }
                }
            })
        });
    }else if(action=="certified") {
        //状态变更
        var alertstr = "未知操作";
        var isCertified = 0;
        if (data.isCertified == 0){
            alertstr = "是否将该茶艺师变更为：已认证？";
            isCertified = 1;
        } else if (data.isCertified == 1) {
            alertstr = "是否将该茶艺师变更为：未认证？";
            isCertified = 0;
        }
        swal({
            title: "状态变更",
            text: alertstr,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false
        }, function () {
            //点击删除之后此处进行传递后台
            //创建传递的参数
            var postdata = {
                action:"certified",
                id: data.id,
                isCertified:isCertified,
            };
            $.ajax({
                url:pxt+"/save",
                type:"post",
                data:JSON.stringify(postdata),
                contentType:"application/json",
                dataType:"json",
                success:function(data){
                    if (data.code === "200") {
                        $('#myModalAdd').modal('hide')
                        doSearch();
                        swal("状态已变更!", "请继续操作", "success");
                    } else {
                        swal("操作失败!", "请重试", "error");
                    }
                }
            })
        });
    }

}

//给控件赋值****************************
function WriteData(data) {
    $("#text_name").val(data.name);
    $("#text_phone").val(data.phone);
    $("#text_amount").val(data.amount);
    $("#text_star").val(data.star);
    $("#text_synopsis").val(data.synopsis);
    url = data.imageUrl;
    getImage(url);
    $("#text_lng").val(data.lng);
    $("#text_lat").val(data.lat);
    $("#text_scope").val(data.scope);
}
//上传****************************
function ImportShipmentStatusList() {
    var timestamp = new Date().getTime();
    var upname = "fuImportMultipleShipmentStatus";
    console.log(timestamp);
    console.log("upname:"+$("#" + upname).val());
    if ($("#" + upname).val() == "") {
        alert("请选择上传图片");
        return;
    }
    $.ajaxFileUpload({
        url: '/rotation/uploadImages',
        secureuri: false,
        fileElementId: upname,
        dataType: 'json',
        success: function (data, status) {
            url = data.url;
            getImage(data.httpUrl);
        },
        error: function (data, status, e) {
            console.log("上传图片失败",e);
        }

    });
}

function getImage(url) {
    $("#myimage").html("");
    var html="";
    // html=html+"<div class=\"row\">";
    html=html+"<div class=\"col-lg-9\">";
    html=html+"<div class=\"form-group\" style='width:420px; height:250px'>";
    html=html+"<img style='width:420px; height:250px' src=\""+url+"\" />";
    html=html+"<span class=\"label label-danger pull-right\" style=\"cursor:pointer\" onclick=\"delimage();\">删除</span>";
    html=html+"</div>";
    html=html+"</div>";
    html=html+"</div>";
    $("#myimage").append(html);
}
function delimage() {
    //清空file
    var test = document.getElementById('fuImportMultipleShipmentStatus');
    test.value = '';
    $("#myimage").html("");
}
//绑定下拉****************************

//*****************************************************************************************
